package it.unipr.ce.dsg.s2p.example.peerdroid;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class My_join_peer  extends Activity implements OnClickListener{
	private TextView myIPTabJoin;
	private EditText joinBootstrap;
	private Button sendToJoin;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.my_join_peer);
		
		myIPTabJoin = (TextView)findViewById(R.id.txtMyIpTabJoin);
		joinBootstrap = (EditText)findViewById(R.id.edtJoinBootstap);
		sendToJoin = (Button)findViewById(R.id.btnSendToJoin);
		
		
		if(PeerActivity.peer!=null){
			myIPTabJoin.setText(PeerActivity.peer.getAddressPeer().toString());//display IP
			if(PeerActivity.peer.getBootstrapPeer()!=null){//Display bootstrap
				joinBootstrap.setText(PeerActivity.peer.getBootstrapPeer().getURL());
				
				sendToJoin.setOnClickListener(this);//button action
			}
//			else{
//				joinBootstrap.setText("None");
//			}
		}//end if PeerActivity.peer!=null
		
		//do some more
	}//end onCreate
	
	 public void onClick(View v){
	        if(v instanceof Button){
	             switch(v.getId()){
	             case R.id.btnSendToJoin:{
	                  //do something
	            	 sendToJoinAction();
	                  break;
	             }
	             
	             
	             }//end switch
	        }// end if -instance of Button
	        
	       }//end onClick
	
	public void sendToJoinAction(){
		//if user input new bootstrapIP --> update bootstrapIP
		String oldBootstrap = 	PeerActivity.peer.getBootstrapPeer().getURL();
		String newBootstrap = joinBootstrap.getText().toString();
		if(!newBootstrap.contentEquals(oldBootstrap)){
			PeerActivity.peer.setConfiguration(newBootstrap);//if different, update new bootstrap IP
			Toast.makeText(this,"Update new Bootstrap " + newBootstrap , Toast.LENGTH_LONG)
			.show();
		}
		PeerActivity.peer.joinToPeer(PeerActivity.peer.getBootstrapPeer());
//		finish();
	}
}//end My_join_peer

